package cn.luoshaocheng.basic.PocketMQUtil;

import cn.luoshaocheng.pet.domain.SearchMasterMsg;
import cn.luoshaocheng.pet.mapper.SearchMasterMsgMapper;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "queue_test_topic",selectorExpression="*",consumerGroup = "queue_group_test")
public class RocketMQMsgListener implements RocketMQListener<MessageExt> {
@Autowired
private SearchMasterMsgMapper searchMasterMsgMapper;
    @Override
    public void onMessage(MessageExt message) {
        System.out.println(1);
        byte[] body = message.getBody();
        String msg = new String(body, CharsetUtil.UTF_8);
        Long aLong = Long.valueOf(msg);

        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(aLong);
        System.out.println(new Date().toLocaleString());
        if (searchMasterMsg.getState()!=2){
            searchMasterMsgMapper.remove(aLong);
        }
    }

}